package com.agritech.machine;

import com.agritech.environment.EnvironmentData;
import com.agritech.observer.EnvironmentObserver;

/**
 * 播种机观察者类
 * 实现环境观察者接口，根据环境数据调整播种机的工作模式
 */
public class SeederObserver implements EnvironmentObserver {

    @Override
    public String getObserverName() {
        return "播种机观察者 (" + seeder.getId() + ")";
    }
    private Seeder seeder;
    
    public SeederObserver(Seeder seeder) {
        this.seeder = seeder;
    }
    
    @Override
    public void update(EnvironmentData data) {
        System.out.println("播种机 " + seeder.getId() + " 收到环境数据更新:");
        
        // 根据环境数据调整播种机工作模式
        if (data.getSoilMoisture() < 30) {
            System.out.println("检测到土壤湿度低 (" + data.getSoilMoisture() + "%)，增加播种深度");
            seeder.adjustSeedingDepth(seeder.getSeedingDepth() + 1.0);
        } else if (data.getSoilMoisture() > 80) {
            System.out.println("检测到土壤湿度高 (" + data.getSoilMoisture() + "%)，减少播种深度");
            seeder.adjustSeedingDepth(Math.max(1.0, seeder.getSeedingDepth() - 1.0));
        }
        
        if (data.getSoilPH() < 5.5 || data.getSoilPH() > 7.5) {
            System.out.println("检测到土壤pH值异常 (" + data.getSoilPH() + ")，暂停播种作业");
            if (seeder.isRunning()) {
                seeder.stop();
            }
        }
    }
}